// alert(9)
$(function () {
  template.defaults.imports.dateFormat = function (dateStr) {
    // console.log(dateStr)
    var dt = new Date(dateStr)
    var y = dt.getFullYear()
    var m = padZero(dt.getMonth() + 1)
    var d = padZero(dt.getDate())

    var hh = padZero(dt.getHours())
    var mm = padZero(dt.getMinutes())
    var ss = padZero(dt.getSeconds())

    // return 'yyyy-mm-dd'
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
  }
  // 补零函数
  function padZero (n) {
    return n < 10 ? '0' + n : n
  }
  //新闻列表页
  //文章列表加载功能
  var news = {
    pagenum: 1,
    pagesize: 8,
  };
  //渲染
  list_news();
  function list_news() {
    $.ajax({
      method: "GET",
      url: "http://127.0.0.1:8888/api/articles",
      data: news,
      success(res) {
        console.log(res);
        if (res.status !== 0) {
          return layui.layer.msg(res.message, { icon: 5 });
        }
        var htmlstr = template("news", res);
        $(".kr_news_list").html(htmlstr);
        renderPage(res.total);
      },
    });
  }
  //分页
  function renderPage(total) {
    layui.laypage.render({
      elem: "",
      count: total,
      limit: news.pagesize,
      curr: news.pagenum,
      layout: ["count", "prev", "page", "next", "limit", "refresh", "skip"],
      limits: [2, 4, 6, 8],
      jump: function (obj, first) {
        if (first === true) {
          return;
        }
        news.pagenum = obj.curr;
        news.pagesize = obj.limit;
        list_news();
      },
    });
  }
  renderPage();
  //动态加载更多功能
  $(".kr_more").on("click", function () {
    news.pagesize = news.pagesize + 3;
    list_news();
  });
});
